package com.ningkang.auth.service.impl;
import com.alibaba.fastjson.JSON;
import com.ningkang.auth.domain.LoginParam;
import com.ningkang.auth.domain.LoginResponse;
import com.ningkang.auth.remote.UserServiceRemote;
import com.ningkang.auth.service.AuthService;
import com.ningkang.common.constants.JwtConstants;
import com.ningkang.common.constants.TokenConstants;
import com.ningkang.common.domain.ums.UserVo;
import com.ningkang.common.result.Result;
import com.ningkang.common.utils.JwtUtils;
import com.ningkang.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName AuthServiceImpl
 * @Description 描述
 * @Author guoxin
 * @Date 2024/10/17 19:48
 */
@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private UserServiceRemote userServiceRemote;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Override
    public LoginResponse login(LoginParam loginParam) {
        Result<UserVo> userVoResult = userServiceRemote.selectName(loginParam.getUsername());
        if (userVoResult.getCode()!=200){
            throw new RuntimeException(userVoResult.getMsg());
        }
        UserVo userVo = userVoResult.getData();
        if (!userVo.getPassword().equals(loginParam.getPassword())){
            throw new RuntimeException("密码错误");
        }
        //生成token令牌
        String userKey = UUID.randomUUID().toString().replaceAll("-", "");
        String token = JwtUtils.createToken(new HashMap<String, Object>() {{
            put(JwtConstants.USER_KEY, userKey);
        }});
        stringRedisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY +userKey, JSON.toJSONString(userVo),30, TimeUnit.MINUTES);
        LoginResponse loginResponse = new LoginResponse();
        loginResponse.setToken(token);
        loginResponse.setTokenTime("15min");
        return loginResponse;
    }

    /* *
     * @Title: getUserInfo
     * @Author: DELL
     * @Date: 2024-10-18 10:30:29
     * @Params: []
     * @Return: Result<UserVo>
     * @Description: 获取登录信息
     */
    @Override
    public UserVo getUserInfo() {
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        String userJson = stringRedisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        if (StringUtils.isBlank(userJson)){
            throw new RuntimeException("token过期");
        }
        return JSON.parseObject(userJson, UserVo.class);
    }

    /* *
     * @Title: logout
     * @Author: DELL
     * @Date: 2024-10-18 10:31:00
     * @Params: []
     * @Return: Result
     * @Description: 登出功能
     */
    @Override
    public void logout() {
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        stringRedisTemplate.delete(TokenConstants.LOGIN_TOKEN_KEY + userKey);

    }
}
